import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';
import querystring from 'querystring';

let LOADED = false;
export default modelEnhance({
  namespace: 'room',
  state: {
    pageData: PageHelper.create(),
    houseId: 0,
    house: {}
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/house/room' && !LOADED) {
          LOADED = true;
          // 获取路由参数
          dispatch({
            type: 'setHouseId',
            payload: { houseId: querystring.parse(history.location.search.replace('?', '')).id }
          });
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },
  effects: {
    // 进入页面加载
    *init({ payload }, { call, put, select }) {
      const { pageData } = yield select(state => state.room);
      yield put({
        type: 'getPageInfo',
        payload: {
          pageData: pageData.startPage(1, 10),
        }
      });
      yield put({
        type: 'getHouse'
      });
    },
    *getHouse({ payload }, { call, put, select }) {
      const { houseId } = yield select(state => state.room);
      yield put({
        type: '@request',
        payload: {
          valueField: 'house',
          url: '/project_case/house/' + houseId + '/',
          method: 'get'
        },
        afterResponse: (resp) => resp.data,
      })
    },
    // 获取分页数据
    *getPageInfo({ payload }, { call, put, select }) {
      // 获取state数据
      const { houseId } = yield select(state => state.room);
      const { pageData } = payload;
      yield put({
        type: '@request',
        payload: {
          valueField: 'pageData',
          url: '/project_case/room/?house=' + houseId,
          pageInfo: pageData,
          method: 'get'
        }
      });
    },
    *save({ payload }, { call, put, select, take }) {
      const { values, record, success } = payload;
      console.log('record', record)
      console.log('values', values)
      const { pageData, houseId } = yield select(state => state.room);
      // put是非阻塞的 put.resolve是阻塞型的
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: record ? `/project_case/room/${record.id}/` : '/project_case/house/' + houseId + '/add_room/',
          data: values,
          method: record ? 'patch' : 'post',
          afterResponse: (resp) => {
            success()
          }
        }
      });
      success();
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    },
    *update({ payload }, { call, put }) { },
    // 删除 之后查询分页
    *remove({ payload }, { call, put, select }) {
      const { records, success } = payload;
      const { pageData } = yield select(state => state.room);
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: `/project_case/room/${records[0].id}/`,
          method: 'delete'
        }
      });
      success();
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
    },
  },
  reducers: {
    setHouseId(state, { payload }) {
      const { houseId } = payload
      let _state = state;
      _state.houseId = houseId
      return state
    }
  }
});